package 标准接口;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;
import cn.hutool.json.JSONArray;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;




public class CreateRequestinfo {

//第一步做一次就行了  记录下来
    /**
     * 模拟缓存服务
     */
    private static final Map<String,String> SYSTEM_CACHE = new HashMap<>();

    /**
     * ecology 系统发放的授权许可证(appid)
     */
    private static final String APPID = "1e9bb811-bf63-4770-8dc1-a9f737fca11f";


    public static void main(String[] args) {

        String jsonString = HttpRequest.get("http://oa.maxwell-gp.com.cn:8090/api/fc_zh_pms/get_params?param_name=hrm_bymobile&mobile=18706207589").execute().body();

        System.out.println(jsonString);
        String out;
        Object succesResponse = JSON.parse(jsonString);    //先转换成Object
        Map map = (Map)succesResponse;
        String oaid="";
        JSONArray jsonArray = new JSONArray(map.get("result"));
        //只取第一笔在职的
        for( int i=0;i<jsonArray.size();i++){
            System.out.println("第"+(i+1)+"条开始.........");
            System.out.println(jsonArray.get(i));
            JSONObject info = new JSONObject((Map<String, Object>) jsonArray.get(i));
            if("在职".equals(info.get("status"))){
                oaid=(String) info.get("id");
                break;
            }
        }

        System.out.println(oaid+"////");


/*
        for( int i=0;i<jsonArray.size();i++){
            System.out.println("第"+(i+1)+"条开始.........");
            System.out.println(jsonArray.get(i));
            JSONObject info = new JSONObject((Map<String, Object>) jsonArray.get(i));
            System.out.print("requestid:"+info.get("requestid")+"    ");
            System.out.println("nodename:"+info.get("nodename"));
            System.out.println("dt1物料信息明细:"+info.get("dt1"));
            JSONArray mx1 = new JSONArray(info.get("dt1").toString());
            for( int j=0;j<mx1.size();j++){
                JSONObject dt1info = new JSONObject((Map<String, Object>) mx1.get(j));
                System.out.print("   "+(j+1)+":LH:"+dt1info.get("LH")+"    ");
                System.out.println("GG:"+dt1info.get("GG"));
            }
            System.out.println("dt2临时对策:"+info.get("dt2"));

        }
        */
        //Object succesResponse = JSON.parse(data);
        //System.out.println(succesResponse);


       // System.out.println("testRestful()："+map1);



// ?param_name=hrm_bymobile&mobile=13063863511

       // HttpRequest.closeCookie();
        //testRestful("http://oa.maxwell-gp.com:8090","/api/workflow/paService/doCreateRequest","");

        //testGetoken("http://oa.maxwell-gp.com:8090");
    }
    /**
     * 第一步：
     *
     * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
     */

    public static Map<String,Object> testGetoken(String address){
        // 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息

        String secret ="338f95ad-4c89-405d-9334-c1e72e2cda97";
        String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCACpqY2eJhiYOIsuIgl/+Y6F5uVsAIoZRlozLXe4CvZ55Y+Exxc3Df1AY2UMorIMnGmPQ+rZkfkQQO3OJXJ1hBrhB2x2EPoOAdB8brSyQ2Z6jMhP8Fl7AYVlTm+iHS+vmXNXkgO+YD1itsnp1AYg2wIlPf+dj/tRWEgwbOqwaefQIDAQAB";

        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
        //ECOLOGY 返回的系统密钥
        SYSTEM_CACHE.put("SERVER_SECRET",secret);

        RSA rsa = new RSA(null,spk);
        //对秘钥进行加密传输，防止篡改数据
        String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        System.out.println(encryptSecret);
        //调用 ECOLOGY 系统接口进行注册
        String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
                .header("appid",APPID)
                .header("secret",encryptSecret)
                .header("time","3600")
                .execute().body();
        System.out.println("testGetoken()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY 返回的 token
        // TODO 为 Token 缓存设置过期时间
        SYSTEM_CACHE.put("SERVER_TOKEN", StrUtil.nullToEmpty((String)datas.get("token")));
        return datas;
    }

    public static String testRestful(String address,String api,String jsonParams){

        //ECOLOGY 返回的 token
        String token=(String) testGetoken(address).get("token");
        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");

        //封装请求头参数
        RSA rsa = new RSA(null,spk);
        //对用户信息进行加密传输,暂仅支持传输 OA 用户 ID
        String encryptUserid = rsa.encryptBase64("21574",CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口
        String data = HttpRequest.post(address + api)
                .header("appid",APPID)
                .header("token",token)
                .header("userid",encryptUserid)
                .body("workflowId=2042&requestName=测试马上删&"
                                +"mainData = ["
                                + "{'fieldName':'KHMC','fieldValue':'271'},"
                                + "{'fieldName':'KDRQ','fieldValue':'2024-05-28'},"
                                + "{'fieldName':'JXMC','fieldValue':'46'},"
                                + "{'fieldName':'YSRQ1','fieldValue':'2024-11-28'},"
                                + "{'fieldName':'jxbh','fieldValue':'测试'},"
                                + "{'fieldName':'neiorwai','fieldValue':'0'},"
                                + "{'fieldName':'xqwllb','fieldValue':'0'},"
                                + "{'fieldName':'zcfzr','fieldValue':'15551'},"
                                + "{'fieldName':'mwkf','fieldValue':'15551'},"
                                + "{'fieldName':'sbzt','fieldValue':'0'},"
                                + "{'fieldName':'blms','fieldValue':'测试丝印物料必填物料补发'},"
                                + "{'fieldName':'fj10','fieldValue':[{'fileName':'迈为.jpg','filePath':'https://plt.maxwell-gp.com.cn:8056/galaxy/maxwell-review-server/review/api/file/previewImg?filePath=mwgroup/M00/00/23/wKgAg2ZVeCCAeHflAAAeTai5BZI570.jpg'}] }]"
                                +"&detailData = "
                                +"[{'tableDBName':'formtable_main_50_dt4','workflowRequestTableRecords':["
//每一行开始
                                + "{'recordOrder':0,'workflowRequestTableFields':["
                                + "{'fieldName':'LH','fieldValue':'120211011160'},"
                                + "{'fieldName':'GG','fieldValue':'FUF-40GTR 470X15'},"
                                + "{'fieldName':'erp_leibie','fieldValue':'机械标准件'},"
                                + "{'fieldName':'SL','fieldValue':'1'},"
                                + "{'fieldName':'sysj','fieldValue':'1'},"
                                + "{'fieldName':'pm','fieldValue':'皮带'},"
                                + "{'fieldName':'yichang','fieldValue':'异常'},"
                                + "{'fieldName':'erp_zhonglei','fieldValue':'中类'},"
                                + "{'fieldName':'zkdjsjh','fieldValue':'3'}"//这个前台丝印没用到
                                + "]},"
//每一行结束
                                + "]}]"

                )

                .execute().body();

        System.out.println("token:"+token);
        System.out.println("userid:"+encryptUserid);
        System.out.println("testRestful()："+data);

        return data;
    }



}
